package com.example.demo.controller;

import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author shenb
 * @date 2022-06-28 18:28
 */
@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping
    public List<User> list() {
        List<User> users = jdbcTemplate.query("select * from user_inf ", new BeanPropertyRowMapper<>(User.class));
        return users;
    }

    @PostMapping
    public User addUser() {
        User user = new User();
        jdbcTemplate.update("insert into user_inf (name, age) values (?, ?)", "tom", 12);
        return user;
    }
}
